package com.example.customview.silder

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.view.View
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.customview.R
import com.example.customview.SmartUtil


class ProvinceCityItemDecoration<T>() : RecyclerView.ItemDecoration() {
    private var groupHeight = 30F
    private var lineHeight = 1f
    private val mPaint = Paint()
    private val mLinePaint = Paint()
    private val textPaint = Paint()
    private val rect = Rect()
    private var letterLeft =20f
    private val data = arrayListOf<T>()
    private val dp8 = SmartUtil.dp2px(0f)
    private var hasHeadView: Boolean = false

    constructor(
        context: Context,
        groupHeight: Int,
        lineHeight: Int,
        data: List<T>,
        hasHeadView: Boolean = false
    ) : this() {
        this.hasHeadView = hasHeadView
        val displayMetrics = context.resources.displayMetrics
        this.groupHeight = groupHeight * displayMetrics.density
        this.lineHeight = lineHeight * displayMetrics.density
        this.letterLeft = (letterLeft * displayMetrics.density+0.5).toFloat()
        mPaint.color = Color.WHITE
        mPaint.isAntiAlias = true
        mLinePaint.color = ContextCompat.getColor(context, R.color.color_25272b_16)
        mLinePaint.isAntiAlias = true
        textPaint.color = ContextCompat.getColor(context, R.color.color_20242b_64)
        textPaint.textSize = 11 * displayMetrics.density
        textPaint.textAlign = Paint.Align.LEFT
        textPaint.isAntiAlias = true
        this.data.addAll(data)
    }
    //绘制字母
    override fun onDrawOver(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDrawOver(c, parent, state)
        val childCount = parent.childCount
        val left = 0f
        val right = parent.right.toFloat()
        val top = 0f
        for (index in 0 until childCount) {
            val layoutManager = parent.layoutManager as LinearLayoutManager
            var position = layoutManager.findFirstVisibleItemPosition()
            val itemView =
                parent.findViewHolderForAdapterPosition(position)?.itemView
            position -= 1
            if (position < 0)
                continue
            val isGroup = isGroupName(position + 1)

            val groupName = when (val t = data[position]) {
                is ProvinceDtoItem -> {
                    t.letter?.substring(0, 1) ?: ""
                }
                is CityDtoItem -> {
                    t.letter?.substring(0, 1) ?: ""
                }
                else -> {
                    "#"
                }
            }

            if (isGroup) {
                val bottom = groupHeight.coerceAtMost(itemView?.bottom?.toFloat() ?: 0f)
                c.drawRect(left, top, right, bottom, mPaint)
                textPaint.getTextBounds(groupName, 0, groupName.length, rect)
                c.drawText(
                    groupName, letterLeft,
                    top + bottom - groupHeight / 2 + rect.height() / 2, textPaint
                )
            } else {
                c.drawRect(left, top, right, top + groupHeight, mPaint)
                textPaint.getTextBounds(groupName, 0, groupName.length, rect)
                c.drawText(
                    groupName, letterLeft,
                    top + groupHeight / 2 + rect.height() / 2, textPaint
                )
            }
        }
    }

    private fun isGroupName(position: Int): Boolean {
        if (position < 0)
            return false
        return if (position == 0) {
            true
        } else {
            val letter = when (val t = data[position]) {
                is ProvinceDtoItem -> {
                    t.letter?.substring(0, 1) ?: ""
                }
                is CityDtoItem -> {
                    t.letter?.substring(0, 1) ?: ""
                }
                else -> {
                    "#"
                }
            }

            val nextLetter = when (val t = data[position - 1]) {
                is ProvinceDtoItem -> {
                    t.letter?.substring(0, 1) ?: ""
                }
                is CityDtoItem -> {
                    t.letter?.substring(0, 1) ?: ""
                }
                else -> {
                    "#"
                }
            }
//            val letter = data[position].letter?.substring(0, 1) ?: ""
//            val nextLetter = data[position - 1].letter?.substring(0, 1) ?: ""
            letter != nextLetter
        }

    }

    override fun onDraw(c: Canvas, parent: RecyclerView, state: RecyclerView.State) {
        super.onDraw(c, parent, state)
        val childCount = parent.childCount
        val left = SmartUtil.dp2px(20f).toFloat()
        val right = parent.right.toFloat()-SmartUtil.dp2px(20f)
        for (index in 0 until childCount) {
            val childView = parent.getChildAt(index)
            val top = childView.top
            //省份有head
            val childLayoutPosition = if (data[0] is CityDtoItem) {//
                parent.getChildLayoutPosition(childView)
            } else {//省份
                if (hasHeadView) {
                    parent.getChildLayoutPosition(childView) - 1
                } else {
                    parent.getChildLayoutPosition(childView)//无定位
                }
            }
            if (childLayoutPosition < 0)
                continue
            val isGroup = isGroupName(childLayoutPosition)
            val t = data[childLayoutPosition]
            val groupName = when (t) {
                is ProvinceDtoItem -> {
                    t.letter?.substring(0, 1) ?: ""
                }
                is CityDtoItem -> {
                    t.letter?.substring(0, 1) ?: ""
                }
                else -> {
                    "#"
                }
            }
            if (isGroup) {
                textPaint.getTextBounds(groupName, 0, groupName.length, rect)
                if (childLayoutPosition > 0) {
                    c.drawRect(
                        left,
                        childView.top - groupHeight + 6,
                        right,
                        childView.top.toFloat() - groupHeight + 8,
                        mLinePaint
                    )
                }
                c.drawText(
                    groupName, letterLeft,
                    childView.top - groupHeight / 2 + rect.height(),
                    textPaint
                )
            }
        }
    }

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {

        //省份有head
        if (data[0] is CityDtoItem) {
            val position = parent.getChildLayoutPosition(view)
            if (position >= 0) {
                val sameGroup = isGroupName(position)
                if (sameGroup) {
                    outRect.set(0, groupHeight.toInt() + dp8, 0, 0)
                }
            }
        } else {
            val position = if (hasHeadView) {
                parent.getChildLayoutPosition(view) - 1
            } else {
                parent.getChildLayoutPosition(view)//无定位
            }
            if (position >= 0) {
                val sameGroup = isGroupName(position)
                if (sameGroup) {
                    outRect.set(0, groupHeight.toInt() + dp8, 0, 0)
                }
            }
        }
    }
}